const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateShow:false,
    currentDate: new Date().getTime(),
    minDate: new Date(1999,12).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    date:'本月',
    revenueDatailList:'',
    pageNum:1,
    income:'',
    outcome:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.currentDate,'currentDate-------')
    // 明细
    this.revenueDatail();
  },

  selectDate(){
    this.setData({
      dateShow: true,
    });
  },

  onClose(){
    this.setData({
      dateShow: false,
    });
  },

  onInput(event) {
    console.log(event,'event---------')
    this.setData({
      currentDate: event.detail,
      pageNum:1,
      revenueDatailList:''
    });
    this.onClose();
    this.revenueDatail();
  },
  // 日期
  bindDateChange: function (e) {
    console.log(e,'eeeeeeeeee')
    this.setData({
      date: e.detail.value,
      pageNum:1,
      revenueDatailList:''
    })
    this.revenueDatail();
  },

  formatDate: function (value) {//调用时间戳为日期显示
    let date = new Date(value)
    let y = date.getFullYear()  //获取年份
    let m = date.getMonth() + 1  //获取月份
    m = m < 10 ? "0" + m : m  //月份不满10天显示前加0
    return y + "-" + m
  },

  // 明细
  revenueDatail:function(){
    var _this = this;
    var { currentDate, pageNum, revenueDatailList } = this.data

    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'pay/financeLists',
      data: {
        token: app.globalData.token,
        timeStatus: this.formatDate(currentDate),
        page: pageNum
      },
      success(res) {
        wx.hideLoading()
        console.log(res.data.data.finance,'data.data.finance----')
        if (res.data.code == 1000) {
          let data=res.data.data.finance
          data.forEach(item=>{
            if(item.action == 1 || item.action == 4 || item.action == 5 || item.action == 6 || item.action == 16){
              item.cashText='-'+item.cash
            }else {
              item.cashText='+'+item.cash
            }
          })
          _this.setData({
            revenueDatailList: [...revenueDatailList, ...res.data.data.finance],
            income:res.data.data.income,
            outcome:res.data.data.outcome,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var { pageNum } = this.data;
    pageNum ++;
    this.setData({
      pageNum
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
